package com.example.algorithm.tree;

/**
 * @author : zhy
 * @since : 2022/7/30
 * 二叉树最小深度
 */
public class TreeMinDeep {

    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);
        if (root.left == null) {
            return rightDepth + 1;
        }
        if (root.right == null) {
            return leftDepth + 1;
        }
        // 左右结点都不为null
        return Math.min(leftDepth, rightDepth) + 1;

    }
}
